package com.huifaapp.mall.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.base.activity.BaseActivity;
import com.huifaapp.mall.base.basemvp.IPresenter;
import com.huifaapp.mall.bean.WelcomeBean;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class WelcomeActivity extends BaseActivity {
    @BindView(R.id.iv_log)
    ImageView ivLog;
    private boolean isFirstIn = false;
    //添加6.0以上权限
//    String[] permissions = new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.WRITE_APN_SETTINGS};
//    List<String> mPermissionList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }



    @Override
    protected void initData(Bundle bundle) {
//        LoadImage();
        //申请6.0以上的权限
     //   shenqingsix();
        initAnimator();


    }

//    private void shenqingsix() {
//        /**
//         * 判断哪些权限未授予
//         */
//        mPermissionList.clear();
//        for (int i = 0; i < permissions.length; i++) {
//            if (ContextCompat.checkSelfPermission(mContext, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
//                mPermissionList.add(permissions[i]);
//            }
//        }
//
//        /**
//         * 判断是否为空
//         */
//        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
////            delayEntryPage();
//        } else {//请求权限方法
//            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
//            ActivityCompat.requestPermissions(WelcomeActivity.this, permissions, 1);
//        }
//
//
//
//
//    }
//
//    boolean mShowRequestPermission = true;//用户是否禁止权限
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        switch (requestCode) {
//            case 1:
//                for (int i = 0; i < grantResults.length; i++) {
//                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
//                        //判断是否勾选禁止后不再询问
//                        boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this, permissions[i]);
//                        if (showRequestPermission) {//
//                        shenqingsix();//重新申请权限
//                            return;
//                        } else {
//                            mShowRequestPermission = false;//已经禁止
//                        }
//                    }
//                }
//
//                break;
//            default:
//                break;
//        }
//    }
    private  void LoadImage(){
        HttpUtils http = new HttpUtils();
        Log.e("数据","图片路径："+ApiManager.WELCOME_IMAGE);
        http.send(HttpRequest.HttpMethod.POST,ApiManager.WELCOME_IMAGE, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {

                Log.e("数据", "数据-------" + current + "/" + total);
            }


            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Log.e("数据123", "数据d-------" + responseInfo.result);
                Gson gson = new Gson();

                WelcomeBean bean = gson.fromJson(responseInfo.result, WelcomeBean.class);
                Log.e("数据12", "数据data-------" + bean.getImg());
                uploadimageview(ivLog,bean.getImg(),null,WelcomeActivity.this);




            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }
    public static void uploadimageview(final ImageView views, String url, String bili, Context c) {
        DisplayImageOptions HXOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        ImageLoader.getInstance().displayImage(url, views, HXOptions);
    }


    @Override
    protected IPresenter onLoadPresenter() {
        return null;
    }


    private void initAnimator() {


        AlphaAnimation anima = new AlphaAnimation(0.1f, 1.0f);
        anima.setDuration(3000);// 设置动画显示时间

        anima.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }
            @Override
            public void onAnimationEnd(Animation animation) {
                SharedPreferences preferences = getSharedPreferences("first_pref",
                        MODE_PRIVATE);
                isFirstIn = preferences.getBoolean("isFirstIn", true);
                // 如果是第一次启动，则跳转到引导页面，否则继续往下执行
                if (isFirstIn) {
                    // start guideactivity
                    Intent intent = new Intent(WelcomeActivity.this,
                            IntroductionPageActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    //做对应的操作，比如登录
                    LoadIsShow();
//                    Intent intent = new Intent(WelcomeActivity.this,WelcomeActivityone.class);
//                    startActivity(intent);
                }
                SharedPreferences preferences1 = getSharedPreferences("first_pref",
                        MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("isFirstIn", false);
                editor.commit();

        }


            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        ivLog.startAnimation(anima);


    }
    private void LoadIsShow() {
        String url = ApiManager.PROJECT_WELCOM;
        Log.e("主题","主题url===="+url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject obj=new JSONObject(responseInfo.result);
                    if(obj.getString("code").equals("10000")) {
                        if(obj.getString("isShow").equals("hide")){
                            Intent intent = new Intent(WelcomeActivity.this,MainFragment.class);
                            startActivity(intent);
                        }else{
                            Intent intent = new Intent(WelcomeActivity.this,WelcomeActivityone.class);
                            intent.putExtra("url",obj.getString("url"));
                            startActivity(intent);
                        }
                    }else {
                        Intent intent = new Intent(WelcomeActivity.this,MainFragment.class);
                        startActivity(intent);
                        Toast.makeText(WelcomeActivity.this, obj.getString("msg"), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override

            public void onFailure(HttpException e, String s) {
                Toast.makeText(WelcomeActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    protected boolean translucentStatusBar() {
        return true;
    }

}
